<?php
header('Content-type:text/html;charset=utf-8');
global $_W,$_GPC;
if($_W['container'] != 'wechat' || $_W['os'] != 'mobile'){
    message('本系统仅支持在手机端微信打开','','info');
    exit;
}
checkauth();

$_result = array();

$Qrcode = new \Controller\Qrcode();
try{
    switch ($_GPC["scan_type"]) {
        // 申请发红包权限
        case 'red_access':
            $RedAccess = new \Controller\RedAccess();
            $_status = $RedAccess->value("status");
            if(!$_status || $_status == 9) {
                if($_status == 9 ? $RedAccess->edit(array("status"=>1,"time"=>TIMESTAMP)) : $RedAccess->add()) {
                    $_result = array("code"=>0,"err_msg"=>"已申请发红包权限，等待审核");
                } else {
                    $_result = array("code"=>103,"err_msg"=>"申请发红包权限失败，请重新操作");
                }
            } else {
                $_result = array("code"=>103,"err_msg"=>$_status == 1 ? '请不要重复申请，耐心等待审核' : '您已经有发红包权限了');
            }
            break;
        // 核销卡券
        case 'check_coupon':
            $_member_coupon_id = $_GPC["member_coupon_id"];
            $MemberCoupon = new \Controller\MemberCoupon();
            $Coupon = new \Controller\Coupon();
            // 卡券信息
            $_member_coupon = $MemberCoupon->get($_member_coupon_id);
            // 0、验证卡券核销权限
            // 0.1、如果不是卡券拥有者，则验证是不是卡券拥有者的员工
            $_owner_uid = $Coupon->value("uid",$_member_coupon["coupon_id"]);
            if($_owner_uid != \Kuyuan\WqCore\util\Util::uid()){
                // 获取员工
                $Staff = new \Controller\Staff();
                if(!$Staff->isStaff($_owner_uid)){
                    throw new Exception("无核销权限",805);
                }
            }

            // 1、验证卡券状态
            if($_member_coupon["status"] == 2){
                throw new Exception("卡券已核销",804);
            }
            // 1.1、验证卡券是否过期
            if($_member_coupon["past"]) {
                throw new Exception("卡券已过期",804);
            }

            // 2 核销，修改状态
            if($MemberCoupon->edit(array("status"=>2),$_member_coupon_id)) {
                throw new Exception("核销成功",0);
            } else {
                throw new Exception("核销失败，请重试",103);
            }
            break;
        // 申请员工
        case 'apply_staff':
            if(\Kuyuan\WqCore\util\Util::uid() == $_GPC["boss"] && !KUYUAN_DEBUG) {
                throw new Exception("不能申请成为自己的员工",801);
            }
            $_params = array(
                "boss"=>$_GPC["boss"],
                "scan_type"=>$_GPC["scan_type"],
                "uniqid" => $_GPC["uniqid"]
            );
            if($Qrcode->checkParams($_params)) {
                $Staff = new \Controller\Staff();
                $_staff = $Staff->testStaff($_GPC["boss"]);
                if($_staff["status"] == 1) {
                    throw new Exception("您已经是员工了",801);
                } else if($_staff["status"] == 2) {
                    throw new Exception("正在审核中",801);
                } else if($_staff["status"] == 9) {
                    if(!$Staff->edit(array("status"=>2,"time"=>TIMESTAMP),$_staff["id"])) {
                        throw new Exception("申请员工失败，请重新扫码",103);
                    } else {
                        throw new Exception("申请成功，等待审核",0);
                    }
                } else if(!$_staff) {
                    if(!$Staff->add(array("boss"=>$_GPC["boss"]))) {
                        throw new Exception("申请员工失败，请重新扫码",103);
                    } else {
                        throw new Exception("申请成功，等待审核",0);
                    }
                }
            } else {
                throw new Exception("二维码失效",800);
            }
            break;
        // 设置管理员
        case 'set_manage':
            $_params = array(
                "scan_type"=>$_GPC["scan_type"],
                "uniqid" => $_GPC["uniqid"]
            );
            if($Qrcode->checkParams($_params)) {
                \Kuyuan\WqCore\cache\Cache::name(KUYUAN_NAME."_manage_uid",\Kuyuan\WqCore\util\Util::uid());
                throw new Exception("扫码成功",0);
            } else {
                throw new Exception("二维码失效",800);
            }
            break;
        default:
            throw new Exception("非法操作",404);
    }
}catch (\Exception $e) {
    $_result = array("code"=>$e->getCode(),"err_msg"=>$e->getMessage());
}


$_script = \Tool\wx\Wx::jsapi();
$_script .= "<script>wx.hideAllNonBaseMenuItem();</script>";

include $this->template('scan');

